package com.kzd.gtgame.main.money

import android.annotation.SuppressLint
import android.webkit.WebView
import androidx.annotation.DrawableRes
import androidx.lifecycle.LifecycleOwner
import com.kzd.gtgame.R
import com.kzd.gtgame.app.URL_MALL_INTRODUCE
import com.kzd.gtgame.custom.createViewWeb
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.entity.MallDetails
import com.kzd.gtgame.jetpack.vm.MoneyViewModel
import com.kzd.gtgame.main.KEY_MONEY_MALL_ID
import com.kzd.gtgame.main.checkLogin
import com.kzd.gtgame.main.toMoneyMallSubActivity
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.image.loadImage
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_money_mall_det.*
import java.lang.StringBuilder

class MoneyMallDetActivity : OwnerViewModeActivity<MoneyViewModel>() {

    private val mMallId: String by lazy {
        intent?.extras?.getString(KEY_MONEY_MALL_ID) ?: "0"
    }

    private val mWebView: WebView by lazy {
        createViewWeb(context = this)
    }

    override fun viewModel(): MoneyViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_money_mall_det

    override fun stateBar(): StatusBar {
        return StatusBar(view = flMallDetailsBar)
    }

    override fun initView() {
        webMallDetailsBody.addView(mWebView)

        tvMallDetailsCollect.setOnClickAntiShakeListener {
            val isCollect = viewModel.moneyTaskMallDetailsLiveData.value
                ?.data?.data?.is_shoucang ?: 0
            if (isCollect == 0) checkLogin {
                viewModel.userCollectAdd(mMallId, type = 1)
            } else checkLogin {
                viewModel.userCollectDel(mMallId, type = 1)
            }
        }

        tvMallDetailsSubmit.setOnClickAntiShakeListener {
            checkLogin { viewModel.moneyTaskMallQuery(mMallId) }
        }
    }

    override fun initData() {
        viewModel.moneyTaskMallDetails(mMallId)
    }

    override fun initViewMode() {
        viewModel.moneyTaskMallDetailsLiveData.observe(this as LifecycleOwner, { response ->
            mslMultiStatusLayout.setLoadingStatus(response)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            fillData(data)
        })

        viewModel.userCollectAddLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            viewModel.moneyTaskMallDetailsLiveData.value?.data?.data?.apply {
                this.is_shoucang = 1
            }
            setDrawableTopCollect(R.drawable.ic_10002)
            toast(response.data?.message)
        })

        viewModel.userCollectDelLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            viewModel.moneyTaskMallDetailsLiveData.value?.data?.data?.apply {
                this.is_shoucang = 0
            }
            setDrawableTopCollect(R.drawable.ic_10001)
            toast(response.data?.message)
        })

        viewModel.moneyTaskMallQueryLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val mallDetails = viewModel.moneyTaskMallDetailsLiveData.value
                ?.data?.data ?: return@observe
            toMoneyMallSubActivity(mallDetails)
        })

    }

    private fun fillData(mallDetails: MallDetails) {
        ivMallDetailsImage.loadImage(mallDetails.goods_thumb)
        tvMallDetailsGoodsName.text = mallDetails.goods_name
        pbMallDetailsNumPro.progress = mallDetails.bfb
        tvMallDetailsNumber.text = StringBuilder().append("剩余数量：")
            .append(mallDetails.goods_number)
        val url = StringBuilder(URL_MALL_INTRODUCE)
            .append("&id=").append(mMallId).toString()
        mWebView.loadUrl(url)
        setDrawableTopCollect(
            if (mallDetails.is_shoucang == 1) R.drawable.ic_10002 else R.drawable.ic_10001
        )
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private fun setDrawableTopCollect(@DrawableRes id: Int) {
        val top = resources.getDrawable(id, null)
        top.setBounds(0, 0, top.minimumWidth, top.minimumHeight)
        tvMallDetailsCollect.setCompoundDrawables(null, top, null, null)
    }
}